#include "user.h"
#include <QDebug>
#include "main.h"

user::user(QString nid,QString nname,QString nip,QString njob,QPixmap npicture):
    id(nid),name(nname),ip(nip),job(njob),picture(npicture)
  // , chat(NULL)
{
    //msg = new message(chat, name,"暂时没有新消息",picture,"16:15");
    //smsg = new small_message(chat, name, picture);
    // ChatWidget w(db.GetFriend(id.toInt()));
    if(nname.isEmpty()){
        picture = QPixmap::fromImage(init_picture(nname[0]));
    }
    else{
        picture = QPixmap::fromImage(init_picture(nname.right(1).at(0)));
    }
}

void user::set_name(QString new_name){
    name = new_name;
    picture = QPixmap::fromImage(init_picture(name.right(1).at(0)));
}
void user::set_ip(QString new_ip){
    ip = new_ip;
}
void user::set_picture(QString path){
    picture = QPixmap(path);
}
QImage user::init_picture(QChar text){
    QImage image(100, 100, QImage::Format_RGB32);
    image.fill(Qt::black);  // 设置背景为黑色

    QPainter painter(&image);
    painter.setPen(Qt::white);  // 设置字体颜色为白色
    painter.setFont(QFont("Arial", 30));  // 设置字体大小和样式

    painter.drawText(image.rect(), Qt::AlignCenter, text);  // 在图像中心绘制文字

    return image;
}


void user::show_chat(){
    if(chat&&(!chat->visibleRegion().isEmpty())){
        qDebug() <<"弹出原聊天框";
        chat->activateWindow();
        chat->setWindowState((chat->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
        QScrollBar *scrollBar = chat->scrollarea->verticalScrollBar();
        scrollBar->setValue(scrollBar->maximum());
    }
    else{
        qDebug() <<"产生新聊天框";
        //chat_widget_list[id.toInt()]=&w;
        Friend recv,sender;

        recv.id=id.toInt();
        recv.ip=ip;
        recv.job=job;
        recv.name=name;
        user* receive = new user(QString::number(recv.id), recv.name, recv.ip, recv.job);

        sender.id = use_uid.toInt();
        sender.ip=  use_ip;
        sender.job= "myself";
        sender.name = use_name;
        user* send = new user(QString::number(sender.id), sender.name, sender.ip, sender.job);
        chat = new ChatWidget(sender,recv,send->picture,receive->picture);
        QVector<Message> recent_message;
        // 区分群聊还是好友
        //        if (id[0]!='8')
        recent_message  = db.GetSomeRecentMessageByid(sender.id, recv.id, 20);
        //        else
        //    recent_message  = db.GetSomeRecentMessageByGroupid(recv.id, 20);
        qDebug() << recent_message.size();
        for(auto message=recent_message.rbegin();message !=recent_message.rend(); message++){
            if(message->msg.contains("##")){
                if(message->msg.section("##",2,2)!="1"){
                    if (message->tid!=sender.id){
                        QJsonObject file_bag;
                        file_bag.insert("UID", QString::number( message->rid));
                        file_bag.insert("MID",QString::number( message->id));
                        file_bag.insert("content",message->msg);
                        QJsonDocument jsonDocument;
                        jsonDocument.setObject(file_bag);
                        chat->beforeReceiveFile(message->msg.section("##",0,0) ,jsonDocument.toJson(),message->msg.section("##",1,1).toInt());
                    }
                    else{
                         chat->sendMessage("您发送了文件："+message->msg.section("##",0,0));
                    }
                }
                else{
                    if (message->tid!=sender.id){
                        QJsonObject file_bag;
                        file_bag.insert("UID", QString::number( message->rid));
                        file_bag.insert("MID",QString::number( message->id));
                        file_bag.insert("content",message->msg);
                        QJsonDocument jsonDocument;
                        jsonDocument.setObject(file_bag);
                        chat->beforeReceivePicture(message->msg.section("##",0,0) ,
                                                   jsonDocument.toJson(),message->msg.section("##",1,1).toInt());
                    }
                    else{
                         chat->sendMessage("您发送了文件："+message->msg.section("##",0,0));
                    }
                }
            }
            else{
                if (message->tid==sender.id)
                    chat->sendMessage(message->msg);
                else
                    chat->receiveMessage(message->msg);
            }
        }
        chat->show();
        QScrollBar *scrollBar = chat->scrollarea->verticalScrollBar();
        scrollBar->setValue(scrollBar->maximum());
    }
}
void user::show_info(){
    if(info&&(!info->visibleRegion().isEmpty())){
        qDebug() <<"弹出信息框";
        info->activateWindow();
        info->setWindowState((info->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
    }
    else{
        qDebug() <<"产生新信息框";
        //chat_widget_list[id.toInt()]=&w;
        info = new personinfo(id);
        info->set_head(picture);
        info->set_ip(ip);
        info->set_job(job);
        info->set_name(name);

        info->show();
    }
}

